package com.lqd.demo.springboot.file;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
public class FileUploadAndDownloadController {

	@RequestMapping("/fileIndex")
	public String fileIndex()
	{
		return "/file";
	}
	
	@RequestMapping("/batch/fileIndex")
	public String batchFileIndex()
	{
		return "/mutifile";
	}
	
	@RequestMapping("/upload")
	@ResponseBody
	public String fileUpload(@RequestParam("filename") MultipartFile file){
		
		if (file.isEmpty())
		{
			return "false" ;
		}	
		String fileName = file.getOriginalFilename() ;
		System.out.println("----------" + fileName);
		
		String filePath = "D://" ;
		File dest = new File(filePath + fileName);
		if (!dest.getParentFile().exists())
		{
			dest.getParentFile().mkdirs();
		}	
		try {
			file.transferTo(dest);
			return "true";
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "false";
	}
	
	@RequestMapping(value="/batch/upload",method = RequestMethod.POST)
	@ResponseBody
	public String batchFileUpload(HttpServletRequest request){
		
		List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("filename") ;
		String filePath = "D://" ;
		if (!CollectionUtils.isEmpty(files))
		{
			for (MultipartFile multipartFile : files) {
				
				String fileName = multipartFile.getOriginalFilename() ;
				System.out.println("----------" + fileName);
				
				File dest = new File(filePath + fileName);
				
				try {
					multipartFile.transferTo(dest);
				} catch (Exception e) {
					e.printStackTrace();
					return "false" ;
				}
			}
		}	
		
		return "true";
	}
	
	@RequestMapping("/download")
	public @ResponseBody String downloadFile(HttpServletResponse resp) {

		String filePath = "D://" ;
		String fileName = "731889208.docx";
		File file = new File(filePath,fileName);
		if (file.exists())
		{
			resp.setContentType("application/force-download");
			resp.addHeader("Content-Disposition", "attachment;fileName="+fileName);
			
			byte[] buffer = new byte[1024] ;
			FileInputStream fis = null;
			BufferedInputStream bis = null;
			OutputStream os = null;
			try {
				fis = new FileInputStream(file);
				bis = new BufferedInputStream(fis);
				int i = bis.read(buffer) ;
				os = resp.getOutputStream() ;
				while (i!=-1){
					os.write(buffer);
					i = bis.read(buffer) ;
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				return "false" ;
			} finally {
				
				try {
					fis.close();
					bis.close();
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
		}	
		return "true" ;
	}
}
